<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class news extends Admin_Controller
{
	var $_table_name = NEWS;
	function lists()
	{
		global $CI;
		//check language
		$language = $CI->uri->segment(3);
		if($language=="english"){
			$this->_table_name = $this->_table_name."_en";			
		}
		// model
		$CI->load->module_model('news.news_list_model');
		// sql
		$q = $CI->db->get($this->_table_name);
		
		// pagenavigation
		$CI->load->library('pagination');
        $config['base_url'] = site_url($CI->news_list_model->url_prefix.'/%s');
        $config['total_rows'] = $q->num_rows();
        $config['per_page'] = $CI->config->item('rows_per_page');
		$config['cur_page'] = $CI->news_list_model->start;
		$config['num_links'] = $CI->config->item('num_links');		
        $CI->pagination->initialize($config);
        $this->add('paging', $CI->pagination->create_links());
		
		// sql
		$CI->db->select('*');
		$CI->db->from($this->_table_name);				    	
		foreach ($CI->news_list_model->columns as $k=>$v)
		{
			if ($CI->news_list_model->cache->load($k))
				$CI->db->order_by($v, $CI->news_list_model->cache->load($k));
		}
		$CI->db->limit($CI->config->item('rows_per_page'), $CI->news_list_model->start);
		$q = $CI->db->get();		
		
		
		//Get image path
		$CI->load->library("fckeditor");	
		$path = $CI->fckeditor->UploadPath.'/images/';
		$this->add('image_franchise_path', $path);
		$items = $q->result();				
		foreach($items as &$item){
			$item->image_path = $path.$item->{NEWS_IMAGE};
		}
		// gridview
		$this->add('items', $items);
		$this->add('start', $CI->news_list_model->start);
		$this->add('language', $language?$language:'');
		foreach ($CI->news_list_model->columns as $k=>$v)
		{
			if ($CI->news_list_model->cache->load($k))
				$this->add('sort_'.$k,  'class="sort_column_'.$CI->news_list_model->cache->load($k).'"');
		}

		// message
		$this->add('msg', $CI->news_list_model->cache->load($CI->news_list_model->msg_cache_prefix));
		$CI->news_list_model->cache->add($CI->news_list_model->msg_cache_prefix, '');
		
		// toolbar
		$this->module_toolbar('news.toolbar', array('parent_id'=>0, 'start'=>$CI->news_list_model->start, 'language'=>$language));
		$this->module_navigation('Quản lý tin tức ');
		
		// view
		$this->view('news_list_view');
	}
	
	function detail()
	{
		global $CI;
		
		// model
		$CI->load->module_model('news.news_detail_model');
		//check language
		$language = $CI->uri->segment(3);
		$thetopic_table = TOPIC;
		if($language=="english"){
			$this->_table_name = $this->_table_name."_en";			
			$thetopic_table .= "_en";
		}
		$this->add('language', $language?$language:'');
		// Get All topic
		$q = $CI->db->get($thetopic_table);
		$topics = $q->result();
		
		
		$q = $CI->db->get_where($this->_table_name, array(NEWS_NEWS_ID=>$CI->news_detail_model->item));
		$news = $q->row();
		
			
		// error
		$post = $CI->news_detail_model->cache->load('post');
		$CI->news_detail_model->cache->add('post', '');

		// add to view
		$this->add('title', $post?$post['title']:($news?$news->{NEWS_TITLE}:''));
		$this->add('topic', $post?$post['topic']:($news?$news->{NEWS_TOPIC}:''));
		$this->add('topics', $topics);
		//$this->add('image', $post?$post['class_duration']:($news?$news->{COURSE_CLASS_DURATION}:''));
		$this->add('active', $post?(isset($post['active'])?' checked':''):($news?($news->{NEWS_STATUS}?' checked':''):' checked'));
		$this->add('imagepath', $post?$post['imagepath']:($news?$news->{NEWS_IMAGE}:''));
		$this->add('imagejson', $post?$post['imagejson']:($news?$news->{NEWS_IMAGE_JSON}:''));
		
		$this->add('start', $CI->news_detail_model->start);
		$this->add('item', $CI->news_detail_model->item);
		$this->add('language', $language?$language:'');
		
		/*
		// stage listbox
		$CI->load->module_model('stage.stage_listbox_model');
		$CI->stage_listbox_model->name = 'stage_ids[]';
		$CI->stage_listbox_model->news = $CI->news_detail_model->item;
		if ($post)
			$CI->stage_listbox_model->values = $post['stage_ids'];
		else {
			$q = $CI->db->get_where(COURSE_STAGE, array(COURSE_STAGE_FRANCHISE_ID=>$CI->news_detail_model->admin_id(), COURSE_STAGE_COURSE_ID=>$CI->news_detail_model->item));
			$values = array();
			foreach ($q->result() as $row)
				$values[] = $row->{COURSE_STAGE_STAGE_ID};
			$CI->stage_listbox_model->values = $values;
		}
		*/
		
		// editor
		$CI->load->library('fckeditor');
		$CI->fckeditor->InstanceName = 'description';
		$CI->fckeditor->Value = $post?$post['description']:($news?$news->{NEWS_DESCRIPTION}:'');
		$CI->fckeditor->SetPathOfFranchise();
		$this->add('description', $CI->fckeditor->CreateHtml());
		
		// editor
		$CI->load->library('fckeditor');
		$CI->fckeditor->InstanceName = 'review';
		$CI->fckeditor->Value = $post?$post['review']:($news?$news->{NEWS_REVIEW}:'');
		$CI->fckeditor->SetPathOfFranchise();
        $CI->fckeditor->Height = 140;
		$this->add('review', $CI->fckeditor->CreateHtml());
				
		// message
		$this->add('msg', $CI->news_detail_model->cache->load($CI->news_detail_model->msg_cache_prefix));
		$CI->news_detail_model->cache->add($CI->news_detail_model->msg_cache_prefix, '');
		
		// toolbar
		$this->module_toolbar();
		if ($CI->news_detail_model->item)
			$this->module_navigation('Tin Tức : HIệu chỉnh');
		else
			$this->module_navigation('Tin tức : Thêm mới');
			
		// view
		$this->view('news_detail_view');
	}
	
}

?>